package mwa.cs.cr.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.AddressDAO;
import mwa.cs.dao.ChargeRateDAO;
import mwa.cs.dao.CustHHDAO;
import mwa.cs.dao.CustMonthlyDAO;
import mwa.cs.dao.CustomerClassDAO;
import mwa.cs.dao.CustomerDAO;
import mwa.cs.dao.CustomerHistoryLogDAO;
import mwa.cs.dao.DefaultAddressDAO;
import mwa.cs.dao.DeptRateDAO;
import mwa.cs.dao.GenDocDAO;
import mwa.cs.dao.MeterBrandDAO;
import mwa.cs.dao.MeterSizeDAO;
import mwa.cs.dao.RouteDAO;
import mwa.cs.dao.RouteDetailDAO;
import mwa.cs.dao.SMErrorLogDAO;
import mwa.cs.dao.SMListOfValueDAO;
import mwa.cs.dao.ZoneDAO;
import mwa.cs.database.Database;
import mwa.cs.hh.biz.MeterFunction;
import mwa.cs.model.ChargeRate;
import mwa.cs.model.CustMonthly;
import mwa.cs.model.Customer;
import mwa.cs.model.CustomerHistoryLog;
import mwa.cs.model.DefaultAddress;
import mwa.cs.model.RouteDetail;
import mwa.cs.util.JDate;

/**
 * Servlet implementation class CS21001Srvl
 */
public class CS21001Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int vnSiteId;
	private int vnUserId;
	private String vaUrlAddress;
	private String vaAgentBrowser;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS21001Srvl() {
        super();
    }

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// get site_id
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
		// see what button is clicked?
		String vaButtonUser = request.getParameter("button_user");
		String vaButtonMeter = request.getParameter("button_meter");
		// get URL_ADDRESS, AGENT_BROWSER
		vaUrlAddress = request.getParameter("url_address");
		vaAgentBrowser = request.getParameter("agent_browser");

		String vaProcessType = request.getParameter("process_type");
		if (vaProcessType != null) {
			if (vaProcessType.equals("gotoShowData")) {
				showData(request, response);
				return;
			} else if (vaProcessType.equals("getData")) {
				getCustomerData(request, response);
				return;
			}
		}
		
		// get customer info
		String vaAccount = request.getParameter("account");
		String vaZone = "0";
		String vaRoute = "0";
		String vaSequence = "0";
		String vaPName = request.getParameter("pname").trim();
		String vaFName = request.getParameter("fname").trim();
		String vaLName = request.getParameter("lname").trim();
		String vaCardId = request.getParameter("card_id").trim();
		String vaTaxId = request.getParameter("tax_id").trim();
		String vaTaxBranch = request.getParameter("tax_branch").trim();
		String vaHouseId = request.getParameter("house_id").trim();
		String vaHomeNumber = request.getParameter("home_number").trim();
		String vaAddress = request.getParameter("address").trim();
		String vaCityCode = request.getParameter("city_code");
		String vaDistrictCode = request.getParameter("district_code");
		String vaProvinceCode = request.getParameter("province_code");
		String vaPostCode = request.getParameter("post_code").trim();
		String vaTelephone = request.getParameter("telephone").trim();
		String vaContactHouseNumber = request.getParameter("contact_home_number").trim();
		String vaContactAddress = request.getParameter("contact_address").trim();
		String vaContactCityCode = request.getParameter("contact_city_code");
		String vaContactDistrictCode = request.getParameter("contact_district_code");
		String vaContactProvinceCode = request.getParameter("contact_province_code");
		String vaContactPostCode = request.getParameter("contact_post_code").trim();
		String vaContactTelephone = request.getParameter("contact_telephone").trim();
		// get meter info       
		String vaCustomerType = request.getParameter("customer_type");
		String vaCustomerClass = request.getParameter("customer_class");
		String vaStatus = request.getParameter("status");
		String vaAccountOld = request.getParameter("account_old").trim();
		String vaMeterSize = request.getParameter("meter_size");
		String vaMeterBrand = request.getParameter("meter_brand");
		String vaMeterSerialNumber = request.getParameter("meter_serial_number").trim();
		String vaMeterNumber = request.getParameter("meter_number").trim();
		String vaMeterInstallDate = request.getParameter("meter_install_date");
		String vaMeterReadDate = request.getParameter("meter_read_date");
		String vaAllowance = request.getParameter("allowance");
		String vaDeptAmount = request.getParameter("dept_amount");
		String vaGarbageCharge = request.getParameter("garbage_charge").trim();
		String vaWasteWaterCharge = request.getParameter("wastewater_charge").trim();
		String vaOtherCharge = request.getParameter("other_charge").trim();
		// message
		String successMessage = "";
		String errorMessage = "";
		boolean errorOccur = false;
		// equals "" คือมีการส่งตัวแปรมา 
		// equals null คือไม่มีส่งข้อมูลมา
		
		if (!vaMeterInstallDate.equals(""))  vaMeterInstallDate = JDate.saveDate(vaMeterInstallDate);
		if (!vaMeterReadDate.equals(""))  vaMeterReadDate = JDate.saveDate(vaMeterReadDate);
		
		// change String to int in dept_amount
		if (vaDeptAmount.equals("")) vaDeptAmount = "0";
		int vnDeptAmount = Integer.parseInt(vaDeptAmount);
		if (vaGarbageCharge.equals("")) vaGarbageCharge = "0";
		int vnGarbageCharge = Integer.parseInt(vaGarbageCharge);
		if (vaWasteWaterCharge.equals("")) vaWasteWaterCharge = "0";
		int vnWasteWaterCharge = Integer.parseInt(vaWasteWaterCharge);
		if (vaOtherCharge.equals("")) vaOtherCharge = "0";
		int vnOtherCharge = Integer.parseInt(vaOtherCharge);
		
		Customer voCustomer = new Customer(vnSiteId, vaAccount, vaAccountOld, vaStatus, vaCustomerType, 
				vaCustomerClass, vaPName, vaFName, vaLName, vaCardId, vaTaxId, vaTaxBranch, vaHouseId, 
				vaHomeNumber, vaAddress, vaCityCode, vaDistrictCode, vaProvinceCode, vaPostCode, vaTelephone, 
				vaContactHouseNumber, vaContactAddress, vaContactCityCode, vaContactDistrictCode, vaContactProvinceCode, 
				vaContactPostCode, vaContactTelephone, vaMeterSize, vaMeterBrand, vaMeterSerialNumber, vaMeterNumber, 
				vaMeterInstallDate, vaMeterReadDate, vaAllowance, vnDeptAmount, vnGarbageCharge, vnWasteWaterCharge, vnOtherCharge, 
				0, null, null, 0, null, null);
		
		RouteDetail voRouteDetail = new RouteDetail(vnSiteId, Integer.parseInt(vaZone), Integer.parseInt(vaRoute), 
				Integer.parseInt(vaSequence), vaAccount, vaMeterReadDate, 0, null, null, 0, null, null);
		
		CustMonthly voCustMonthly = new CustMonthly(vnSiteId, Integer.parseInt(vaZone), Integer.parseInt(vaRoute), 
				vaAccount, JDate.getPreviousMonth(JDate.getMonth(), JDate.getYear()), 
				JDate.getYearOfPreviousMonth(JDate.getMonth(), JDate.getYear()), 
				Integer.parseInt(vaSequence), vaCustomerClass, null, 
				vaMeterSerialNumber, vaMeterNumber, vaMeterReadDate, 0, 0, vaAllowance, 0, 0, 0, 0, null, vaStatus, 
				0, null, null, 0, null, null);
		
		// setting success/error message and setting tab that will show
		if ((vaButtonUser.equals("yes") && (vaFName.equals("") || vaLName.equals("") || vaHomeNumber.equals("")))
				|| (vaButtonMeter.equals("yes") && (vaMeterSize.equals("") || vaCustomerClass.equals("") || vaMeterSerialNumber.equals("")
						|| vaMeterReadDate.equals("") || vaMeterNumber.equals("")))) {
			errorMessage = "กรอกข้อมูลไม่สมบูรณ์ กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย '*' ให้ครบถ้วน";
			request.setAttribute("message_error", errorMessage);
			if (vaButtonUser.equals("yes")) {
				request.setAttribute("vaPageSetting", "1");
			} else if (vaButtonMeter.equals("yes")) {
				request.setAttribute("vaPageSetting", "2");
			}
		// ถ้าปุ่มบันทึกผู้ใช้น้ำคลิกแต่หน้าที่ 2 ยังไม่ได้เริ่มใส่ข้อมูลให้ไปหน้าที่ 2
		} else if (vaButtonUser.equals("yes") && (vaMeterSize.equals("") || vaCustomerClass.equals("") || vaMeterSerialNumber.equals("")
				|| vaMeterReadDate.equals("") || vaMeterNumber.equals(""))) {
			request.setAttribute("vaPageSetting", "2");
		// ถ้าปุ่มที่ 2 ถูกคลิกแต่หน้าที่ 1 ยังไม่ได้ใส่
		} else if (vaButtonMeter.equals("yes") && (vaFName.equals("") || vaLName.equals("") || vaHomeNumber.equals(""))) {
			errorMessage = "กรอกข้อมูลผู้ใช้น้ำไม่สมบูรณ์ กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย '*' ให้ครบถ้วน";
			request.setAttribute("message_error", errorMessage);
			request.setAttribute("vaPageSetting", "1");
		// ถ้าใส่ข้อมูลครบแล้ว
		} else {
			Database db = new Database();
			CustomerDAO voCustomerManagement = new CustomerDAO(db);
			RouteDetailDAO voRouteDetailManagement = new RouteDetailDAO(db);
			CustMonthlyDAO voCustMonthlyManagement = new CustMonthlyDAO(db);
			// check setting month and year in cust_monthly (period_month, period_year)
			String vaMaxMonthCustMonthly = null, vaMaxYearCustMonthly = null;
			vaMaxMonthCustMonthly = voCustMonthlyManagement.findMaxMonth(vnSiteId);
			vaMaxYearCustMonthly = voCustMonthlyManagement.findMaxYear(vnSiteId);
			// check account exist or not if exist, updating date else inserting data
			if (vaAccount.equals("")) {
				if (!vaMaxMonthCustMonthly.equals("00")) {
					voCustMonthly.setVaPeriodMonth(vaMaxMonthCustMonthly);
					voCustMonthly.setVaPeriodYear(vaMaxYearCustMonthly);
				}
				db.beginTransaction();
				// generate account
				GenDocDAO voGenDoc = new GenDocDAO(db);
				vaAccount = voGenDoc.generatingNextCRDoc(vnSiteId, JDate.getMonth(), JDate.getYear());
				// set account, meter_date, insert_user_id, insert_date, insert_time
				voCustomer.setVaAccount(vaAccount);
				voCustomer.setVaMeterDate(vaMeterInstallDate);
				voCustomer.setVnInsertUserId(vnUserId);
				voCustomer.setVaInsertDate(JDate.getDate());
				voCustomer.setVaInsertTime(JDate.getTime());
				voRouteDetail.setVaAccount(vaAccount);
				voRouteDetail.setVnInsertUserId(vnUserId);
				voRouteDetail.setVaInsertDate(JDate.getDate());
				voRouteDetail.setVaInsertTime(JDate.getTime());
				voCustMonthly.setVaAccount(vaAccount);
				voCustMonthly.setVnInsertUserId(vnUserId);
				voCustMonthly.setVaInsertDate(JDate.getDate());
				voCustMonthly.setvaInsertTime(JDate.getTime());
				try {
					voCustomerManagement.addCustomer(voCustomer);
					voRouteDetailManagement.addRouteDetail(voRouteDetail);
					voCustMonthlyManagement.insertCustMonthly(voCustMonthly);
					db.commit();
					successMessage = "บันทึกข้อมูลเรียบร้อยแล้ว";
				} catch (Exception e) {
					// errorOccur = true สำหรับเงื่อนไขที่ set Account = "" เวลาผู้ใช้กด save อีกรอบจะได้ไม่เข้าเงื่อนไขอัพเดท
					errorOccur = true;
					try {
						db.rollback();						
						SMErrorLogDAO.getInstance().addLog(0, "CS21001", JDate.getDate(), JDate.getTime(),vnUserId, "InsertCustomerSrvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);					
						errorMessage = "เกิดข้อผิดพลาดในการเพิ่มข้อมูล กรุณาติดต่อผู้ดูแลระบบ";
					} catch (Exception ex) {
						errorMessage = "เกิดข้อผิดพลาด: " + ex.getMessage();
					}
				}
			} else {
				// check updating data
				voCustomer.setVnUpdateUserId(vnUserId);
				voCustomer.setVaUpdateDate(JDate.getDate());
				voCustomer.setVaUpdateTime(JDate.getTime());
				voRouteDetail.setVnUpdateUserId(vnUserId);
				voRouteDetail.setVaUpdateDate(JDate.getDate());
				voRouteDetail.setVaUpdateTime(JDate.getTime());
				voCustMonthly.setVnUpdateUserId(vnUserId);
				voCustMonthly.setVaUpdateDate(JDate.getDate());
				voCustMonthly.setVaUpdateTime(JDate.getTime());
				try {
					db.beginTransaction();
					// Checking History Log
					// finding old_Customer by input vnSiteId, vaAccount 
					Customer voOldCustomer = voCustomerManagement.findCustomerByAccount(vnSiteId, vaAccount);
					// finding old_RouteDetail by input vnSiteId, vaAccount
					RouteDetail voOldRouteDetail = voRouteDetailManagement.findRouteDetailByAccount(vnSiteId, vaAccount);
					voRouteDetail.setVnZoneId(voOldRouteDetail.getVnZoneId());
					voRouteDetail.setVnRouteId(voOldRouteDetail.getVnRouteId());
					voRouteDetail.setVnSeq(voOldRouteDetail.getVnSeq());
					voCustMonthly.setVnZoneId(voOldRouteDetail.getVnZoneId());
					voCustMonthly.setVnRouteId(voOldRouteDetail.getVnRouteId());
					voCustMonthly.setVnSeq(voOldRouteDetail.getVnSeq());
					// check ถ้าเป็นการกรอกข้อมูลผู้ติดตั้งครั้งแรกไม่ต้องบันทึกลง Log
					if (voOldCustomer.getVaMeterSize() != null) {
						// By input db, Old_Cutomer, New_Customer - add difference
						this.checkAndAddHistoryCustomer(vnSiteId, vaAccount, vnUserId, db, voOldCustomer, voCustomer);
						// By input db, Old_RouteDetail, New_RouteDetail - add difference
						this.checkAndAddHistoryRouteDetail(vnSiteId, vaAccount, vnUserId, db, voOldRouteDetail, voRouteDetail);
					} 
					// get max month and year in cust_hh for check update only last cust_monthly
					CustHHDAO voCustHHManagement = new CustHHDAO(db);
					String vaMaxMonthCustHH = voCustHHManagement.findMaxMonth(vnSiteId);
					String vaMaxYearCustHH = voCustHHManagement.findMaxYear(vnSiteId);
					// Start updating
					voCustomerManagement.updateCustomer(vnSiteId, vaAccount, voCustomer);
					voRouteDetailManagement.updateRouteDetail(vnSiteId, vaAccount, voRouteDetail);
					if (vaMaxMonthCustHH.equals("00")) {
						voCustMonthlyManagement.updateCustMonthly(vnSiteId, vaAccount, voCustMonthly, 
								vaMaxMonthCustMonthly, vaMaxYearCustMonthly);
					} else {
						voCustMonthlyManagement.updateCustMonthly(vnSiteId, vaAccount, voCustMonthly, 
								vaMaxMonthCustHH, vaMaxYearCustHH);
					}
					successMessage = "บันทึกข้อมูลเรียบร้อยแล้ว";
					db.commit();
				} catch (Exception e) {
					try {
						db.rollback();
						SMErrorLogDAO.getInstance().addLog(0, "CS0201001", JDate.getDate(), JDate.getTime(),vnUserId, "CS21001Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
						errorMessage = "เกิดข้อผิดพลาดในการอัพเดทข้อมูล กรุณาติดต่อผู้ดูแลระบบ";
					} catch (Exception ex) {
						errorMessage = "เกิดข้อผิดพลาด: " + ex.getMessage();
					}
				}
			}
			// set message
			request.setAttribute("message_success", successMessage);
			request.setAttribute("message_error", errorMessage);
			// if error set account ให้เป็นค่าเริ่มต้น
			if (errorOccur) voCustomer.setVaAccount("");
			// set page to show if it has success message
			// else set page to show error message
			if (successMessage != "") {
				if (vaButtonUser.equals("yes")) {
					request.setAttribute("vaPageSetting", "3");
				} else if (vaButtonMeter.equals("yes")) {
					request.setAttribute("vaPageSetting", "3");
				}
			} else if (errorMessage != "") { 
				if (vaButtonUser.equals("yes")) {
					request.setAttribute("vaPageSetting", "1");
				} else if (vaButtonMeter.equals("yes")) {
					request.setAttribute("vaPageSetting", "2");
				}
			}
			db.close();
		}
		// dispatch back to CS21001
		// set format date
		if (!vaMeterInstallDate.equals(""))  voCustomer.setVaMeterInstallDate(JDate.showDate(vaMeterInstallDate));
		if (!vaMeterReadDate.equals(""))  voRouteDetail.setVaMeterReadDate(JDate.showDate(vaMeterReadDate));
		String vaConsumption = MeterFunction.calculateAverageConsumption(vnSiteId, voCustomer.getVaAccount());
		// set attributes to sending
		request.setAttribute("vaConsumption", vaConsumption);
		request.setAttribute("voCustomer", voCustomer);
		request.setAttribute("voRouteDetail", voRouteDetail);
		// forward back to cs21001.jsp
		RequestDispatcher rd = request.getRequestDispatcher("CS21001.jsp");
		rd.forward(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@SuppressWarnings("unchecked")
	private void getCustomerData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String vaZone = request.getParameter("zone");
		String vaRoute = request.getParameter("route");
		String vaProvince = request.getParameter("province");
		String vaDistrict = request.getParameter("district");
		String vaCity = request.getParameter("city");
		String vaCustomerClass = request.getParameter("customer_class");
		String vaStatus = request.getParameter("status");
		String vaMeterBrand = request.getParameter("meter_brand");
		String vaMeterSize = request.getParameter("meter_size");
		String vaDeptAmount = request.getParameter("dept_amount");
		String vaChargeRate = request.getParameter("charge_rate");
		String vaDefaultAddress = request.getParameter("vaDefaultAddress");
		
		String[] vaMessage = null, vaValue = null,vaArrayDefaultAddress = null;
		Database db = new Database();
		try {
			if ( vaDefaultAddress != null && vaDefaultAddress.equals("yes") ) {
				DefaultAddressDAO voDefaultAddressDAO = new DefaultAddressDAO(db);
				DefaultAddress voDefaultAddress = voDefaultAddressDAO.getDefaultAddressBySiteId(vnSiteId);
				vaArrayDefaultAddress = new String[4];
				vaArrayDefaultAddress[0] = voDefaultAddress.getVaProvinceCode();
				vaArrayDefaultAddress[1] = voDefaultAddress.getVaDistrictCode();
				vaArrayDefaultAddress[2] = voDefaultAddress.getVaCityCode();
				vaArrayDefaultAddress[3] = voDefaultAddress.getVaPostCode();		
			} else if (vaZone != null && vaZone.equals("yes")) {
				ZoneDAO voZone = new ZoneDAO(db);
				List<Map<String, Object>> voResult = voZone.findZoneBySite(vnSiteId);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = ((BigDecimal) voObject.get("ZONE_ID")).toString();
					vaMessage[i] = (String) voObject.get("ZONE_NAME");
				}
			} else if (vaRoute != null && vaRoute.equals("yes") && (!vaZone.equals(""))) {
				RouteDAO voRoute = new RouteDAO(db);
				List<Map<String, Object>> voResult = voRoute.findRouteByZone(vnSiteId, Integer.parseInt(vaZone));
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = ((BigDecimal) voObject.get("ROUTE_ID")).toString();
					vaMessage[i] = (String) voObject.get("ROUTE_DESCRIPTION");		
				}
			} else if (vaProvince != null && vaProvince.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findProvince();
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("PROVINCE"); 
					vaMessage[i] = (String) voObject.get("NAME");		
				}
			} else if (vaDistrict != null && vaDistrict.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findDistrict(vaProvince);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("DISTRICT"); 
					vaMessage[i] = (String) voObject.get("NAME");		
				}			
			} else if (vaCity != null && vaCity.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findCity(vaProvince, vaDistrict);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("CITY"); 
					vaMessage[i] = (String) voObject.get("NAME");
				}	
			} else if (vaCustomerClass != null && vaCustomerClass.equals("yes")) {
				CustomerClassDAO voCustomerClass = new CustomerClassDAO(db);
				List<Map<String, Object>> voResult = voCustomerClass.findCustomerClassBySiteId(vnSiteId);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("CODE");
					vaMessage[i] = (String) voObject.get("DESCRIPTION");
				}
			} else if (vaStatus != null && vaStatus.equals("yes")) {
				SMListOfValueDAO voSmLOV = new SMListOfValueDAO(db);
				List<Map<String, Object>> voResult = voSmLOV.findLOVByType("cust_stts");
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("LOV_CODE");
					vaMessage[i] = (String) voObject.get("LOV_NAME");
				}
			} else if (vaMeterBrand != null && vaMeterBrand.equals("yes")) {
				MeterBrandDAO voMeterBrand = new MeterBrandDAO(db);
				List<Map<String, Object>> voResult = voMeterBrand.findMeterBrandBySiteId(vnSiteId);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("METER_BRAND_CODE");
					vaMessage[i] = (String) voObject.get("METER_BRAND_DESCRIPTION");
				}
			} else if (vaMeterSize != null && vaMeterSize.equals("yes")) {
				MeterSizeDAO voMeterSize = new MeterSizeDAO(db);
				List<Map<String, Object>> voResult = voMeterSize.findMeterSizeBySiteId(vnSiteId);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("METER_SIZE_CODE");
					vaMessage[i] = (String) voObject.get("DESCRIPTION");
				}
			} else if (vaDeptAmount != null && vaDeptAmount.equals("yes")) {
				DeptRateDAO voDeptRate = new DeptRateDAO(db);
				String vaDeptRate = voDeptRate.findDeptAmountByMeterSize(vnSiteId, vaMeterSize);
				vaValue = new String[1];
				vaMessage = new String[1];
				vaValue[0] = vaDeptRate;
				vaMessage[0] = vaDeptRate;				
			} else if (vaChargeRate != null && vaChargeRate.equals("yes")) {
				ChargeRateDAO voChargeRateManagement = new ChargeRateDAO(db);
				ChargeRate voChargeRate = voChargeRateManagement.findChargeRateBySiteId(vnSiteId);
				vaValue = new String[3];
				vaMessage = new String[3];
				vaValue[0] = "garbage_charge";
				vaMessage[0] = Integer.toString(voChargeRate.getVnGarbageCharge());		
				vaValue[1] = "wastewate_charge";
				vaMessage[1] = Integer.toString(voChargeRate.getVnWasteWaterCharge());	
				vaValue[2] = "other_charge";
				vaMessage[2] = Integer.toString(voChargeRate.getVnOtherCharge());	
			}
		} finally {
			db.close();
		}
		PrintWriter out = response.getWriter();
		try {
			JSONObject voObject = new JSONObject();
			JSONArray voList = new JSONArray();
			if (vaMessage != null) {
				for (int i = 0; i < vaMessage.length; i ++) {
					voList.add(vaMessage[i]);
				}
			}
			voObject.put("messages", voList);
			JSONArray voList2 = new JSONArray();
			if (vaValue != null) {
				for (int i = 0; i < vaValue.length; i ++) {
					voList2.add(vaValue[i]);
				}
			}
			voObject.put("values", voList2);		
			JSONArray voList3 = new JSONArray();
			if (vaArrayDefaultAddress != null) {
				for (int i = 0; i < vaArrayDefaultAddress.length; i ++) {
					voList3.add(vaArrayDefaultAddress[i]);
				}
			}
			voObject.put("vaArrayDefaultAddress", voList3);
			out.print(voObject.toJSONString());
		} finally {
			out.close();
		}
	}
	
	private void showData (HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String vaAccount = request.getParameter("customer_account");
		String vaUrlAddress = request.getParameter("url_address");
		String vaAgentBrowser = request.getParameter("agent_browser");
		Database db = new Database();
		try {
			CustomerDAO voCustomerManagement = new CustomerDAO(db);
			RouteDetailDAO voRouteDetailManagement = new RouteDetailDAO(db);
			Customer voCustomer = voCustomerManagement.findCustomerByAccount(vnSiteId, vaAccount);
			RouteDetail voRouteDetail = voRouteDetailManagement.findRouteDetailByAccount(vnSiteId, vaAccount);
			String vaConsumption = MeterFunction.calculateAverageConsumption(vnSiteId, vaAccount);
			// set attributes to sending
			request.setAttribute("vaConsumption", vaConsumption);
			request.setAttribute("voCustomer", voCustomer);
			request.setAttribute("voRouteDetail", voRouteDetail);
			request.setAttribute("vaPageSetting", "3");
			String vaButtonEdit = request.getParameter("button_edit");
			if (vaButtonEdit != null && vaButtonEdit.equals("yes")) {
				request.setAttribute("vaPageSetting", "1");
			}
			RequestDispatcher rd = request.getRequestDispatcher("CS21001.jsp");
			rd.forward(request, response);
			return;
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS23000", JDate.getDate(), JDate.getTime(),vnUserId, "CS21001Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
			request.setAttribute("message_error", "เกิดข้อผิดพลาด กรุณาติดต่อผู้ดูแลระบบ");
			String vaPageSource = request.getParameter("page_source");
			RequestDispatcher rd = null;
			if (vaPageSource != null) {
				if (vaPageSource.equals("CS22000")) {
					rd = request.getRequestDispatcher("CS22000.jsp");
				} else if (vaPageSource.equals("CS23000")) {
					rd = request.getRequestDispatcher("CS23000.jsp");
				}
			} else {
				rd = request.getRequestDispatcher("CS21001.jsp");
			}
			rd.forward(request, response);
		} finally {
			db.close();
		}
	}
	
	private void checkAndAddHistoryCustomer (int pnSiteId, String paAccount, int vnUserId, Database db, 
			Customer poOldCustomer, Customer poNewCustomer) {
		CustomerHistoryLogDAO voCustomerHistoryLogManagement = new CustomerHistoryLogDAO(db);
		AddressDAO voAddress = new AddressDAO(db);
		// pname, fname, lname
		if (poOldCustomer.getVaPName() == null) poOldCustomer.setVaPName("");
		if (poOldCustomer.getVaFName() == null) poOldCustomer.setVaFName("");
		if (poOldCustomer.getVaLName() == null) poOldCustomer.setVaLName("");
		if (poNewCustomer.getVaPName() == null) poNewCustomer.setVaPName("");
		if (poNewCustomer.getVaFName() == null) poNewCustomer.setVaFName("");
		if (poNewCustomer.getVaLName() == null) poNewCustomer.setVaLName("");
		String vaOldName = poOldCustomer.getVaPName() + " " + poOldCustomer.getVaFName() + " " + poOldCustomer.getVaLName();
		String vaNewName = poNewCustomer.getVaPName() + " " + poNewCustomer.getVaFName() + " " + poNewCustomer.getVaLName();
		if (!(vaOldName.equals(vaNewName))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขชื่อผู้ใช้น้ำ" , vaOldName, vaNewName, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// card_id
		if (poOldCustomer.getVaCardId() == null) poOldCustomer.setVaCardId("");
		if (!(poOldCustomer.getVaCardId().equals(poNewCustomer.getVaCardId()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขหมายเลขบัตรประชาชน" , poOldCustomer.getVaCardId(), poNewCustomer.getVaCardId(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// tax_id
		if (poOldCustomer.getVaTaxId() == null) poOldCustomer.setVaTaxId("");
		if (!(poOldCustomer.getVaTaxId().equals(poNewCustomer.getVaTaxId()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขหมายเลขผู้เสียภาษี" , poOldCustomer.getVaTaxId(), poNewCustomer.getVaTaxId(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// tax_branch
		if (poOldCustomer.getVaTaxBranch() == null) poOldCustomer.setVaTaxBranch("");
		if (!(poOldCustomer.getVaTaxBranch().equals(poNewCustomer.getVaTaxBranch()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขสาขาที่ (ผู้เสียภาษี)" , poOldCustomer.getVaTaxBranch(), poNewCustomer.getVaTaxBranch(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// house_id
		if (poOldCustomer.getVaHouseId() == null) poOldCustomer.setVaHouseId("");
		if (!(poOldCustomer.getVaHouseId().equals(poNewCustomer.getVaHouseId()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขรหัสประจำบ้าน" , poOldCustomer.getVaHouseId(), poNewCustomer.getVaHouseId(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// home_number + address + city_code + district_code + province_code + post_code
			// old
		if (poOldCustomer.getVaHomeNumber() == null) poOldCustomer.setVaHomeNumber("");
		if (poOldCustomer.getVaAddress() == null) poOldCustomer.setVaAddress("");
		if (poOldCustomer.getVaPostCode() == null) poOldCustomer.setVaPostCode("");
		String vaOldProvince = voAddress.getProvinceName(poOldCustomer.getVaProvinceCode());
		if (vaOldProvince == null) vaOldProvince = "";
		String vaOldDistrict = voAddress.getDistrictName(poOldCustomer.getVaProvinceCode(), poOldCustomer.getVaDistrictCode());
		if (vaOldDistrict == null) vaOldDistrict = "";
		String vaOldCity = voAddress.getCityName(poOldCustomer.getVaProvinceCode(), poOldCustomer.getVaDistrictCode(), 
				poOldCustomer.getVaCityCode());
        if (vaOldCity == null) vaOldCity = "";
       		// new
		String vaNewProvince = voAddress.getProvinceName(poNewCustomer.getVaProvinceCode());
		if (vaNewProvince == null) vaNewProvince = "";
		String vaNewDistrict = voAddress.getDistrictName(poNewCustomer.getVaProvinceCode(), poNewCustomer.getVaDistrictCode());
		if (vaNewDistrict == null) vaNewDistrict = "";
		String vaNewCity = voAddress.getCityName(poNewCustomer.getVaProvinceCode(), poNewCustomer.getVaDistrictCode(), 
				poNewCustomer.getVaCityCode());
        if (vaNewCity == null) vaNewCity = "";
        	// compare
        String vaOldAddress = poOldCustomer.getVaHomeNumber() + " " + poOldCustomer.getVaAddress() + " " 
                + vaOldCity + " " + vaOldDistrict + " " + vaOldProvince + " " + poOldCustomer.getVaPostCode();
        String vaNewAddress = poNewCustomer.getVaHomeNumber() + " " + poNewCustomer.getVaAddress() + " " 
                + vaNewCity + " " + vaNewDistrict + " " + vaNewProvince + " " + poNewCustomer.getVaPostCode();
		if (!(vaOldAddress.equals(vaNewAddress))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขสถานที่ใช้น้ำ" , vaOldAddress, vaNewAddress, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// telephone
		if (poOldCustomer.getVaTelephone() == null) poOldCustomer.setVaTelephone("");
		if (!(poOldCustomer.getVaTelephone().equals(poNewCustomer.getVaTelephone()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขเบอร์โทรศัพท์ (ที่ใช้น้ำ)" , poOldCustomer.getVaTelephone(), poNewCustomer.getVaTelephone(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// contact - home_number + address + city_code + district_code + province_code + post_code
		if (poOldCustomer.getVaContactHomeNumber() == null) poOldCustomer.setVaContactHomeNumber("");
		if (poOldCustomer.getVaContactAddress() == null) poOldCustomer.setVaContactAddress("");
		if (poOldCustomer.getVaContactPostCode() == null) poOldCustomer.setVaContactPostCode("");
		String vaOldContactProvince = voAddress.getProvinceName(poOldCustomer.getVaContactProvinceCode());
		if (vaOldContactProvince == null) vaOldContactProvince = "";
		String vaOldContactDistrict = voAddress.getDistrictName(poOldCustomer.getVaContactProvinceCode(), 
				poOldCustomer.getVaContactDistrictCode());
		if (vaOldContactDistrict == null) vaOldContactDistrict = "";
		String vaOldContactCity = voAddress.getCityName(poOldCustomer.getVaContactProvinceCode(), 
				poOldCustomer.getVaContactDistrictCode(), poOldCustomer.getVaContactCityCode());
        if (vaOldContactCity == null) vaOldContactCity = "";
       		// new
		String vaNewContactProvince = voAddress.getProvinceName(poNewCustomer.getVaContactProvinceCode());
		if (vaNewContactProvince == null) vaNewContactProvince = "";
		String vaNewContactDistrict = voAddress.getDistrictName(poNewCustomer.getVaContactProvinceCode(), 
				poNewCustomer.getVaContactDistrictCode());
		if (vaNewContactDistrict == null) vaNewContactDistrict = "";
		String vaNewContactCity = voAddress.getCityName(poNewCustomer.getVaContactProvinceCode(), 
				poNewCustomer.getVaContactDistrictCode(), poNewCustomer.getVaContactCityCode());
        if (vaNewContactCity == null) vaNewContactCity = "";
        	// compare
        String vaOldContactAddress = poOldCustomer.getVaContactHomeNumber() + " " + poOldCustomer.getVaContactAddress() + " " 
                + vaOldContactCity + " " + vaOldContactDistrict + " " + vaOldContactProvince + " " + poOldCustomer.getVaContactPostCode();
        String vaNewContactAddress = poNewCustomer.getVaContactHomeNumber() + " " + poNewCustomer.getVaContactAddress() + " " 
                + vaNewContactCity + " " + vaNewContactDistrict + " " + vaNewContactProvince + " " + poNewCustomer.getVaContactPostCode();
		if (!(vaOldContactAddress.equals(vaNewContactAddress))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขสถานที่ติดต่อ" , vaOldContactAddress, vaNewContactAddress, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// contact_telephone
		if (poOldCustomer.getVaContactTelephone() == null) poOldCustomer.setVaContactTelephone("");
		if (!(poOldCustomer.getVaContactTelephone().equals(poNewCustomer.getVaContactTelephone()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขเบอร์โทรศัพท์ (ที่ติดต่อ)" , poOldCustomer.getVaContactTelephone(), poNewCustomer.getVaContactTelephone(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// old_account
		if (poOldCustomer.getVaAccountOld() == null) poOldCustomer.setVaAccountOld("");
		if (!(poOldCustomer.getVaAccountOld().equals(poNewCustomer.getVaAccountOld()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขทะเบียนผู้ใช้น้ำเดิม" , poOldCustomer.getVaAccountOld(), poNewCustomer.getVaAccountOld(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// status
		if (poOldCustomer.getVaStatus() == null) poOldCustomer.setVaStatus("");
		if (!(poOldCustomer.getVaStatus().equals(poNewCustomer.getVaStatus()))) {
			SMListOfValueDAO voSmLOV = new SMListOfValueDAO(db);
			String vaOldStatus = voSmLOV.findStatusByCodeAndType(poOldCustomer.getVaStatus(), "cust_stts");
			String vaNewStatus = voSmLOV.findStatusByCodeAndType(poNewCustomer.getVaStatus(), "cust_stts");
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขสถานะผู้ใช้น้ำ" , vaOldStatus, vaNewStatus, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// customer_type
		if (poOldCustomer.getVaCustomerType() == null) poOldCustomer.setVaCustomerType("");
		if (!(poOldCustomer.getVaCustomerType().equals(poNewCustomer.getVaCustomerType()))) {
			String vaOldCustomerType = "", vaNewCustomerType = "";
			if (poOldCustomer.getVaCustomerType().equals("P")) vaOldCustomerType = "ติดตั้งถาวร";
			else if (poOldCustomer.getVaCustomerType().equals("T")) vaOldCustomerType = "ติดตั้งชั่วคราว";
			if (poNewCustomer.getVaCustomerType().equals("P")) vaNewCustomerType = "ติดตั้งถาวร";
			else if (poNewCustomer.getVaCustomerType().equals("T")) vaNewCustomerType = "ติดตั้งชั่วคราว";
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขประเภทการติดตั้ง" , vaOldCustomerType, vaNewCustomerType, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// customer_class
		if (poOldCustomer.getVaCustomerClass() == null) poOldCustomer.setVaCustomerClass("");
		if (!(poOldCustomer.getVaCustomerClass().equals(poNewCustomer.getVaCustomerClass()))) {
			CustomerClassDAO voCustomerClass = new CustomerClassDAO(db);
			String vaOldCustomerClass = voCustomerClass.findDescriptionBySiteAndCode(pnSiteId, poOldCustomer.getVaCustomerClass());
			String vaNewCustomerClass = voCustomerClass.findDescriptionBySiteAndCode(pnSiteId, poNewCustomer.getVaCustomerClass());
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขประเภทผู้ใช้น้ำ" , vaOldCustomerClass, vaNewCustomerClass, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// allowance
		if (poOldCustomer.getVaAllowance() == null) poOldCustomer.setVaAllowance("");
		if (!(poOldCustomer.getVaAllowance().equals(poNewCustomer.getVaAllowance()))) {
			String vaOldAllowance = "", vaNewAllowance = "";
			if (poOldCustomer.getVaAllowance().equals("0")) vaOldAllowance = "ไม่มี";
			else if (poOldCustomer.getVaAllowance().equals("1")) vaOldAllowance = "มี";
			if (poNewCustomer.getVaAllowance().equals("0")) vaNewAllowance = "ไม่มี";
			else if (poNewCustomer.getVaAllowance().equals("1")) vaNewAllowance = "มี";
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขส่วนลดทหารผ่านศึก" , vaOldAllowance, vaNewAllowance, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// meter_size
		if (poOldCustomer.getVaMeterSize() == null) poOldCustomer.setVaMeterSize("");
		if (!(poOldCustomer.getVaMeterSize().equals(poNewCustomer.getVaMeterSize()))) {
			MeterSizeDAO voMeterSize = new MeterSizeDAO(db);
			String vaOldMeterSize = voMeterSize.findDescriptionBySiteAndCode(pnSiteId, poOldCustomer.getVaMeterSize());
			String vaNewMeterSize = voMeterSize.findDescriptionBySiteAndCode(pnSiteId, poNewCustomer.getVaMeterSize());
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขขนาดมาตร" , vaOldMeterSize, vaNewMeterSize, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// meter_brand
		if (poOldCustomer.getVaMeterBrand() == null) poOldCustomer.setVaMeterBrand("");
		if (!(poOldCustomer.getVaMeterBrand().equals(poNewCustomer.getVaMeterBrand()))) {
			MeterBrandDAO voMeterBrand = new MeterBrandDAO(db);
			String vaOldMeterBrand = voMeterBrand.findDescriptionBySiteAndCode(pnSiteId, poOldCustomer.getVaMeterBrand());
			String vaNewMeterBrand = voMeterBrand.findDescriptionBySiteAndCode(pnSiteId, poNewCustomer.getVaMeterBrand());
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขยี่ห้อมาตร" , vaOldMeterBrand, vaNewMeterBrand, vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// meter_serial_number
		if (poOldCustomer.getVaMeterSerialNumber() == null) poOldCustomer.setVaMeterSerialNumber("");
		if (!(poOldCustomer.getVaMeterSerialNumber().equals(poNewCustomer.getVaMeterSerialNumber()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขหมายเลขมาตร" , poOldCustomer.getVaMeterSerialNumber(), poNewCustomer.getVaMeterSerialNumber(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// meter_install_date
		if (poOldCustomer.getVaMeterInstallDate() == null) poOldCustomer.setVaMeterInstallDate("");
		if (!(poOldCustomer.getVaMeterInstallDate().equals(JDate.showDate(poNewCustomer.getVaMeterInstallDate())))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขวันที่ติดตั้ง" , poOldCustomer.getVaMeterInstallDate(), 
					JDate.showDate(poNewCustomer.getVaMeterInstallDate()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// meter_number
		if (poOldCustomer.getVaMeterNumber() == null) poOldCustomer.setVaMeterNumber("");
		if (!(poOldCustomer.getVaMeterNumber().equals(poNewCustomer.getVaMeterNumber()))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขเลขในมาตร" , poOldCustomer.getVaMeterNumber(), poNewCustomer.getVaMeterNumber(), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// dept_amount
		if (poOldCustomer.getVnDeptAmount() != poNewCustomer.getVnDeptAmount()) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขเงินประกัน" , Integer.toString(poOldCustomer.getVnDeptAmount()), 
					Integer.toString(poNewCustomer.getVnDeptAmount()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// garbage_charge
		if (poOldCustomer.getVnGarbageCharge() != poNewCustomer.getVnGarbageCharge()) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขค่าเก็บขยะ" , Integer.toString(poOldCustomer.getVnGarbageCharge()), 
					Integer.toString(poNewCustomer.getVnGarbageCharge()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// wastewater_charge
		if (poOldCustomer.getVnWasteWaterCharge() != poNewCustomer.getVnWasteWaterCharge()) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขค่าบำบัดน้ำเสีย" , Integer.toString(poOldCustomer.getVnWasteWaterCharge()), 
					Integer.toString(poNewCustomer.getVnWasteWaterCharge()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
		// other_charge
		if (poOldCustomer.getVnOtherCharge() != poNewCustomer.getVnOtherCharge()) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขค่าอื่นๆ" , Integer.toString(poOldCustomer.getVnOtherCharge()), 
					Integer.toString(poNewCustomer.getVnOtherCharge()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}		
	}
	
	private void checkAndAddHistoryRouteDetail (int pnSiteId, String paAccount, int vnUserId, Database db, 
			RouteDetail poOldRouteDetail, RouteDetail poNewRouteDetail) {
		CustomerHistoryLogDAO voCustomerHistoryLogManagement = new CustomerHistoryLogDAO(db);
		// meter_read_date
		if (poOldRouteDetail.getVaMeterReadDate() == null) poOldRouteDetail.setVaMeterReadDate("");
		if (!(poOldRouteDetail.getVaMeterReadDate().equals(JDate.showDate(poNewRouteDetail.getVaMeterReadDate())))) {
			CustomerHistoryLog voCustomerHistoryLog = new CustomerHistoryLog(pnSiteId, paAccount, JDate.getDate(), JDate.getTime(), 
					"แก้ไขวันที่อ่านมาตร" , poOldRouteDetail.getVaMeterReadDate(), 
					JDate.showDate(poNewRouteDetail.getVaMeterReadDate()), vnUserId);
			voCustomerHistoryLogManagement.addCustomerHistoryLog(voCustomerHistoryLog);
		}
	}
	
}


//BigDecimal convert to String and int
//vaValue[i] = ((BigDecimal) voObject.get("ZONE_ID")).toString();
//int a = ((BigDecimal) voObject.get("ZONE_ID")).intValue();